package com.aaa.controller.role;

import com.aaa.entity.ResponseDto;
import com.aaa.service.RoleService;
import com.aaa.service.impl.RoleServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;


@WebServlet("/FindRoleServlet")
public class FindRoleServlet extends HttpServlet {
    private RoleService roleService;

    @Override
    public void init() throws ServletException {
       roleService = new RoleServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ResponseDto responseDto = new ResponseDto();
        try {
            String roleName = req.getParameter("searchName");
            Integer pageSize = Integer.parseInt(req.getParameter("pageSize"));
            Integer pageNum = Integer.parseInt(req.getParameter("pageNumber"));

            Map<String,Object> map = roleService.findRole(pageNum,pageSize,roleName);

            responseDto.setStatus(ResponseDto.SUCCESS_CODE);
            responseDto.setData(map);
            responseDto.setMessage("操作成功");
        }catch (Exception e){
            responseDto.setStatus(ResponseDto.FAILURE_CODE);
            responseDto.setMessage("操作失败");
        }
        resp.getWriter().write(new Gson().toJson(responseDto));




    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
